package server.content.skills.mining;

import server.Server;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.game.players.Client;

public class Rock {

	private final Client c;
	private boolean isDead;
	private final int rockId;
	private final int x;
	private final int y;
	private final int EMPTY_ROCK = 451;
	private int MAX_ORES;
	private int CURR_ORES;
	private int RESPAWN;
	private int CURR_RESPAWN;
	public int XP;
	private int LVL_REQ;
	public int ORE_ID;

	public Rock(final Client c, final int rockId, final int x, final int y) {
		this.c = c;
		this.rockId = rockId;
		this.x = x;
		this.y = y;
		switch (this.rockId) {
		/*
		 * Copper Rocks.
		 */
		case 9708:
		case 9709:
		case 9710:
		case 2090:
		case 2091:
		case 11960:
		case 11962:
		case 11961:
			LVL_REQ = 1;
			MAX_ORES = 1;
			RESPAWN = 8;
			XP = 15;
			ORE_ID = 436;
			break;
		/*
		 * Tin Rocks.
		 */
		case 11957:
		case 11958:
		case 11959:
		case 9714:
		case 9716:
		case 2094:
		case 2095:
			LVL_REQ = 1;
			MAX_ORES = 1;
			RESPAWN = 8;
			XP = 15;
			ORE_ID = 438;
			break;
		/*
		 * Iron Rocks.
		 */
		case 9717:
		case 9718:
		case 9719:
		case 2092:
		case 11956:
		case 11954:
		case 11955:
		case 2093:
			LVL_REQ = 15;
			MAX_ORES = 1;
			RESPAWN = 12;
			XP = 55;
			ORE_ID = 440;
			break;
		/*
		 * Silver Rocks.
		 */
		case 2100:
		case 2101:
		case 11187:
		case 11186:
		case 11188:
			LVL_REQ = 20;
			MAX_ORES = 1;
			RESPAWN = 45;
			XP = 65;
			ORE_ID = 442;
			break;
		/*
		 * Coal Rocks.
		 */
		case 2097:
		case 2096:
			LVL_REQ = 30;
			MAX_ORES = 1;
			RESPAWN = 55;
			XP = 75;
			ORE_ID = 453;
			break;
		/*
		 * Gold Rocks.
		 */
		case 11185:
		case 11184:
		case 11183:
		case 9722:
		case 9720:
		case 2098:
		case 2099:
			LVL_REQ = 40;
			MAX_ORES = 1;
			RESPAWN = 55;
			XP = 85;
			ORE_ID = 444;
			break;
		/*
		 * Mithiral Rocks.
		 */
		case 2103:
		case 2102:
			LVL_REQ = 55;
			MAX_ORES = 1;
			RESPAWN = 95;
			XP = 75;
			ORE_ID = 447;
			break;
	    /*
	     * Adamant Rocks.
	     */
		case 2104:
		case 2105:
			LVL_REQ = 70;
			MAX_ORES = 1;
			RESPAWN = 110;
			XP = 95;
			ORE_ID = 449;
			break;
		/*
		 * Clay.
		 */
		case 11189:
		case 11190:
		case 11191:
		case 9713:
		case 9711:
			LVL_REQ = 1;
			MAX_ORES = 1;
			RESPAWN = 8;
			XP = 35;
			ORE_ID = 434;
			break;
		}
		CURR_ORES = MAX_ORES;
	}

	public void removeOre() {
		if (CURR_ORES > 0) {
			CURR_ORES--;
		} else if (CURR_ORES == 0) {
			Server.objectHandler.createAnObject(c, EMPTY_ROCK, x, y);
			isDead = true;
			CURR_ORES = -1;
			CURR_RESPAWN = RESPAWN;
			process();
		}
	}

	public void process() {
		CycleEventHandler.getSingleton().addEvent(c, new CycleEvent() {

			@Override
			public void execute(CycleEventContainer container) {
				if (CURR_RESPAWN > 0) {
					CURR_RESPAWN--;
				} else if (CURR_RESPAWN == 0) {
					Server.objectHandler.createAnObject(c, rockId, getX(),
							getY());
					isDead = false;
					CURR_ORES = MAX_ORES;
					CURR_RESPAWN = -1;
					container.stop();
				}
			}

			@Override
			public void stop() {

			}
		}, 2);
	}

	public boolean isDead() {
		return isDead;
	}

	public int getLvlReq() {
		return LVL_REQ;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

}
